<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Daemonc extends MY_Controller {
    // Remember to disable CI's csrf-checks for this controller

	function __construct() {
		parent::__construct();
		$this->load->model("daemonc_model");
	}
	
    function index( )
    {
        ignore_user_abort( 1 );
        try
        {
			$this->daemonc_model->runDaemonc();
			return;
			//$this->output->enable_profiler(TRUE);
			//$this->load->model("institute");
			$query = "update ".$this->db->dbprefix."institute left join ".$this->db->dbprefix."city on city_id = ins_city_id and city_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = if(ins_ins_id is null, 'INS', 'FAC') and e2c_ent_id = ins_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set ins_presenter = if(e2c_id is not null, 'I', 'N')";
			$this->db->query($query);
			
			//$this->load->model("accomodation");
			$query = "update ".$this->db->dbprefix."accomodation left join ".$this->db->dbprefix."acc2ins_maj on acc_id = a2im_acc_id and a2im_del = 0 left join ".$this->db->dbprefix."institute on ins_id = a2im_ins_id and ins_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'ACC' and e2c_ent_id = acc_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set acc_presenter = if(e2c_id is not null and ins_presenter = 'I', 'I', 'N')";
			$this->db->query($query);
			
			//$this->load->model("major");
			$query = "update ".$this->db->dbprefix."major left join ".$this->db->dbprefix."institute on ins_id = maj_ins_id and ins_del = 0 and ins_presenter = 'I' left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'MAJ' and e2c_ent_id = maj_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set maj_presenter = if(e2c_id is not null and coalesce(maj_list_from_date, cast('1900-12-31' AS date)) <= curdate() and coalesce(maj_list_till_date, cast('9999-12-31' AS date)) >= curdate() and ins_presenter = 'I', 'I', 'N')";
			$this->db->query($query);
			
			//$this->load->model("scholarship");
			$query = "update ".$this->db->dbprefix."scholarship left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'SCH' and e2c_ent_id = sch_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set sch_presenter = if(e2c_id is not null and coalesce(sch_list_date, cast('9999-12-31' AS date)) >= curdate(), 'I', 'N')";
			$this->db->query($query);
			
			//$this->load->model("country");
			$query = "update ".$this->db->dbprefix."country left join ".$this->db->dbprefix."city on cou_id = city_cou_id and city_del = 0 left join ".$this->db->dbprefix."institute on city_id = ins_city_id and ins_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'COU' and e2c_ent_id = cou_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set cou_presenter = if(e2c_id is not null or ins_presenter = 'I', 'I', 'N'), cou_data = if(e2c_id is not null, 'I', 'N')";
			$this->db->query($query);
			
			//$this->load->model("city");
			$query = "update ".$this->db->dbprefix."city left join ".$this->db->dbprefix."institute on city_id = ins_city_id and ins_del = 0 left join ".$this->db->dbprefix."country on cou_id = city_cou_id and cou_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'CITY' and e2c_ent_id = city_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set city_presenter = if(e2c_id is not null or ins_presenter = 'I', 'I', 'N'), city_data = if(e2c_id is not null, 'I', 'N')";
			$this->db->query($query);
			
			//$this->load->model("daemonc_model");
			$query = "insert into ".$this->db->dbprefix."daemonc (dae_date, dae_cnt) values(curdate(), 1) on duplicate key update dae_cnt = dae_cnt + 1";
			$this->db->query($query);
			
			$this->db->cache_delete_all();
        }
        catch(Exception $e)
        {
            log_message( "error", "Daemon has error: " . $e->getMessage( ) . $e->getFile( ) . $e->getLine( ) );
        }
    }
	
	function run() {
		$this->load->library("daemon");
		$this->daemon->execute_background();
	}
}